﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using WindowsLive.Writer.Api;

namespace Prabir.wlw.Syntaxhiglighter
{
    public partial class PropertyPanelEditor : SmartContentEditor
    {
        IWin32Window _dialogOwner;
        IProperties _pluginOptions;

        public PropertyPanelEditor()
        {
            InitializeComponent();
        }

        private void PropertyPanelEditor_Load(object sender, EventArgs e)
        {
            SelectLanguage();
        }

        public IWin32Window DialogOwner
        {
            get { return _dialogOwner; }
            set { _dialogOwner = value; }
        }
        public IProperties PluginOptions
        {
            get { return _pluginOptions; }
            set { _pluginOptions = value; }
        }

        private void btnEditCode_Click(object sender, EventArgs e)
        {
            EditorActions action = (EditorActions)_pluginOptions.GetInt("EditedAction", (int)EditorActions.DoNothing);

            string code;

            code = SelectedContent.Properties.GetString("Code", "");

            EditForm frm = new EditForm(SelectedContent.Properties.GetString("Lang", "text"), code);

            if (action == EditorActions.SelectAll)
                frm.SelectAll();

            if (frm.ShowDialog(DialogOwner) == DialogResult.OK)
            {
                SelectedContent.Properties.SetString("Lang", frm.Langauge);
                SelectedContent.Properties.SetString("Code", frm.Code);

                // Select appropriate dropdown language.
                SelectLanguage();

                OnContentEdited();
            }

            frm.Dispose();
        }

        private void lnkPrabirDotMe_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(lnkPrabirDotMe.Text);
        }

        protected override void OnSelectedContentChanged()
        {
            SelectLanguage();
        }

        private void SelectLanguage()
        {
            string currentLang = "text";
            if (SelectedContent != null)
                currentLang = SelectedContent.Properties.GetString("Lang", "text");

            foreach (string lang in Syntaxhiglighter.Languages)
            {
                int index = cboLanguage.Items.Add(lang);
                if (lang == currentLang)
                    cboLanguage.SelectedIndex = index;
            }
        }

        private void cboLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectedContent == null)
                return;

            SelectedContent.Properties.SetString("Lang", cboLanguage.Text);
            OnContentEdited();
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            AboutBox ab = new AboutBox();
            ab.ShowDialog(this);
        }

        private void chkCollapse_CheckedChanged(object sender, EventArgs e)
        {
            if (SelectedContent == null)
                return;

            if (chkCollapse.Checked)
                SelectedContent.Properties.SetBoolean("collapse", true);
            else
                SelectedContent.Properties.Remove("collapse");

            OnContentEdited();
        }

        private void chkLineNumbering_CheckedChanged(object sender, EventArgs e)
        {
            if (SelectedContent == null)
                return;

            if (!chkLineNumbering.Checked)
                SelectedContent.Properties.SetBoolean("gutter", false);
            else
                SelectedContent.Properties.Remove("gutter");

            OnContentEdited();
        }

        private void chkAutoLinks_CheckedChanged(object sender, EventArgs e)
        {
            if (SelectedContent == null)
                return;

            if (!chkAutoLinks.Checked)
                SelectedContent.Properties.SetBoolean("auto-links", false);
            else
                SelectedContent.Properties.Remove("auto-links");

            OnContentEdited();
        }

        private void chkToolbar_CheckedChanged(object sender, EventArgs e)
        {
            if (SelectedContent == null)
                return;

            if (!chkToolbar.Checked)
                SelectedContent.Properties.SetBoolean("toolbar", false);
            else
                SelectedContent.Properties.Remove("toolbar");

            OnContentEdited();
        }

        private void chkWrapLines_CheckedChanged(object sender, EventArgs e)
        {
            if (SelectedContent == null)
                return;

            if (!chkWrapLines.Checked)
                SelectedContent.Properties.SetBoolean("wrap-lines", false);
            else
                SelectedContent.Properties.Remove("wrap-lines");

            OnContentEdited();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            if (SelectedContent == null)
                return;

            int val = Convert.ToInt32(numericUpDown1.Value);
            if (val != 1)
                SelectedContent.Properties.SetInt("first-line", val);
            else
                SelectedContent.Properties.Remove("first-line");

            OnContentEdited();
        }


    }
}
